<?php
	include_once("./dal/ProductDAL.php");

	$productUsername = $currentUser->username;
	$txtProductName = "";
	$txtProductBrand = "";
	$txtProductModel = "";
	$txtProductSerialNo = "";
	$txtProductDescription = "";
	$txtProductAmountInStock = "";
	$txtProductUnitPrice ="";
	$newFileName = "";
	$formIsValid = true;
	
	$addProductSuccess = null;
	$productImgUploadSuccess = null;
	
	if(isset($_POST["btnAddProduct"]))
	{

		if($_POST["btnAddProduct"]=="Add")
		{	
			if(isset($_POST["txtProductName"]))
			{
				$txtProductName = $_POST["txtProductName"];
			}
			if(isset($_POST["txtProductBrand"]))
			{
				$txtProductBrand = $_POST["txtProductBrand"];
			}
			if(isset($_POST["txtProductModel"]))
			{
				$txtProductModel = $_POST["txtProductModel"];
			}
			if(isset($_POST["txtProductSerialNo"]))
			{
				$txtProductSerialNo = $_POST["txtProductSerialNo"];
			}
			if(isset($_POST["txtProductDescription"]))
			{
				$txtProductDescription = $_POST["txtProductDescription"];
			}
			if(isset($_POST["txtProductAmountInStock"]))
			{
				$txtProductAmountInStock = $_POST["txtProductAmountInStock"];
			}
			if(isset($_POST["txtProductUnitPrice"]))
			{
				$txtProductUnitPrice = $_POST["txtProductUnitPrice"];
			}
			
			$validFile = true;
			$newFileName = "";
			
			//if a file was uploaded
			if($_FILES['fileProductImg']['name'])
			{
				//if no errors...
				if(!$_FILES['fileProductImg']['error'])
				{
					//now is the time to modify the future file name and validate the file
					$tempFileName = strtolower($_FILES['fileProductImg']['tmp_name']); //rename file
					$newFileName = $_FILES['fileProductImg']['name'];
					$newFileName = $productUsername."_".$newFileName;
					
					if($_FILES['fileProductImg']['size'] > (2048000)) //can't be larger than 2 MB
					{
						$validFile = false;
						$fileUploadMessage = 'Oops!  Your file\'s size is to large.';
					}
					
					//if the file has passed the test
					if($validFile)
					{
						//move it to where we want it to be
						$productImgUploadSuccess = move_uploaded_file($_FILES['fileProductImg']['tmp_name'], getcwd()."/img/product/".$newFileName);
						
						$fileUploadMessage = 'Congratulations!  Your file was accepted.';
						if(!$productImgUploadSuccess)
						{
							$formIsValid = false;
							$validFile = false;
						}
					}
				}
				//if there is an error...
				else
				{
					//set that to be the returned message
					$fileUploadMessage = 'Ooops!  Your upload triggered the following error:  '.$_FILES['fileProductImg']['error'];
					$validFile = false;
				}
			}
			else
			{
				$validFile = false;
			}

			$formIsValid = true;
			if($formIsValid)
			{
				$result = addUserProduct(
					$productUsername,
					$txtProductName,
					$txtProductBrand,
					$txtProductModel,
					$txtProductSerialNo,
					$txtProductDescription,
					$txtProductAmountInStock,
					$txtProductUnitPrice,
					$newFileName
				);
				
				$addProductSuccess = $result;
				
				if($addProductSuccess==true)
				{	
					header(sprintf('Location: products.php?action=newproduct&product=%d&success',$addProductSuccess));
					exit;
				}
				else
				{
					header('Location: products.php?action=newproduct&failed');					
				}
			}			
			
		}
	}
	
	function getContent()
	{	
		global
		$productUsername,
		$txtProductName,
		$txtProductBrand,
		$txtProductModel,
		$txtProductSerialNo,
		$txtProductDescription,
		$txtProductAmountInStock,
		$txtProductUnitPrice,
		$newFileName,
		$formIsValid,
		$addProductSuccess,
		$productImgUploadSuccess;
		
		if(isset($_REQUEST["action"]))
		{
			switch(strtolower($_REQUEST["action"]))
			{
				case 'newproduct':
					if(isset($_REQUEST["success"]))
					{
						if(isset($_REQUEST["product"]))
						{
							$newProductID = $_REQUEST["product"];
							include_once('./templates/AddProductSuccess.php');
						}
					}
					else
					{
						if(isset($_REQUEST["failed"]))
						{
							$addProductSuccess = false;
						}
						include_once('./templates/NewProductForm.php');
					}
				break;
		
				case 'editproduct':
					include_once('./templates/EditProductForm.php');
				break;
				
				default:
					$defaultProducts = getAllProducts();
					include("./templates/DefaultProductsView.php");
				break;
			}
		}
		else
		{
			$defaultProducts = getAllProducts();
			include("./templates/DefaultProductsView.php");
		}
	}
?>